/**业务管理流程平台版本1.0,生成时间:2017-04-13 13:16:48**/
package com.xbdun.dao.bmp.mapper;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.UpdateProvider;

import com.xbdun.dao.bmp.dto.SysUserDto;
import com.xbdun.dao.bmp.dto.open.UserNameAndImgUrlDto;
import com.xbdun.dao.bmp.entity.XbdunSysUserDo;
import com.xbdun.dao.bmp.provider.SysUserSqlProvider;


/**
 * 平台用户信息
 * @author 张国涛
 * @2017-04-13 13:16:48
 */
@Mapper
public interface SysUserMapper{

	/**
	 * 根据主键获取对象
	 * @return
	 */
	@Select("SELECT * FROM xbdun_sys_user where userCode=#{userCode}")
	public XbdunSysUserDo getByUserCode(@Param("userCode")long userCode);
	
	@Select("SELECT * FROM xbdun_sys_user where unionid=#{unionid}")
	public XbdunSysUserDo getByUnionId(@Param("unionid")String unionid);
	
	@Select("SELECT * FROM xbdun_sys_user where idCard=#{idCard}")
	public XbdunSysUserDo getByUserIdCard(@Param("idCard")String idCard);
	/**根据用户编号获取用户信息*/
	@Select("SELECT IFNULL(realName,userName) AS uName, imgUrl,imgUrlFls FROM xbdun_sys_user where userCode=#{userCode}")
	public UserNameAndImgUrlDto getUserNameByCode(@Param("userCode")long userCode);
	/**
	 * 根据用户的身份证获取用户
	 * @return
	 */
	@Select("SELECT userCode FROM xbdun_sys_user where idCard=#{idCard}")
	public Long getByIdCard(@Param("idCard")String idCard);
	/**
	* @Title: getUserCodeByUnion
	* @Description: 根据用户UnionId查询出用户编号
	* @param unionid
	* @return    设定文件
	* @return Long    返回类型
	 */
	@Select("SELECT userCode FROM xbdun_sys_user where unionid=#{unionid}")
	public Long getUserCodeByUnion(@Param("unionid")String unionid);
	
	/**
	 * 新增
	 * @param XbdunSysUserDo 平台用户信息
	 */
	@Insert("INSERT INTO xbdun_sys_user(userCode,userName,pwd,sex,email,realName,birthDate,type,qqOpenid,qq,createTime,orgCode,refNo,refUserCode,refRank,idCard,unionid,xcxOpenId,imgUrl,education,longitude,latitude,regAddr) VALUES(#{a.userCode},#{a.userName},#{a.pwd},#{a.sex},#{a.email},#{a.realName},#{a.birthDate},#{a.type},#{a.qqOpenid},#{a.qq},#{a.createTime},#{a.orgCode},#{a.refNo},#{a.refUserCode},#{a.refRank},#{a.idCard},#{a.unionid},#{a.xcxOpenId},#{a.imgUrl},#{a.education},#{a.longitude},#{a.latitude},#{a.regAddr})")
	public Long addXbdunSysUser(@Param("a")XbdunSysUserDo xbdunSysUserDo);
	
	/**
	 * 删除数据根据主键
	 * @param id
	 */
	@Delete("DELETE FROM xbdun_sys_user WHERE userCode = #{userCode}")
	public void delByUserCode(@Param("userCode")long userCode);
	
	/**
	 * 判断是否存在,不为null则是存在
	 * @param param
	 * @return Long
	 */
	@SelectProvider(type=SysUserSqlProvider.class,method="existSql")
	public Long existXbdunSysUser(@Param("param")Map<String,Object> param);
	/**
	 * 动态更新参数,参数中Map可以必须跟Key为Where的更新条件<br/>
	 * <pre>
	 * 	例如：
	 *  Map<String,Object> setting = new HashMap<String,Object>();
	 *  setting.put("email", "360730440@qq.com");
	 *
	 *  Map<String,Object> where = new HashMap<String,Object>();
	 *  where.put("userId", "1222");
	 * </pre>
	 * Map的key值就是数据库字段<br/>
	 * Map的value就是值
	 */
	@UpdateProvider(type=SysUserSqlProvider.class,method="updateDynXbdunSysUserSql")
	public int updateDynXbdunSysUser(@Param("param")Map<String,Object> param,@Param("_where")Map<String,Object> where);
	
	//------------------------//start分页查询--------------------------------------
	/**
	 * 分页查询 
	 * <pre>
	 * 	例如：
	 *  Map<String,Object> where = new HashMap<String,Object>();
	 *  param.put("orderby", "param.name,param age desc");
	 * </pre>
	 * @param _where where条件，现在支持And条件类型
	 * @param orderBy 排序条件
	 * @param pageNum 当前索引页
	 * @param numPerPage 每页显示条数
	 * @return
	 */
	@SelectProvider(type=SysUserSqlProvider.class,method="queryPagerSql")
	public List<XbdunSysUserDo> queryPager(@Param("_where")Map<String,Object> where,@Param("_orderby")String orderBy,@Param("pageNum")int pageNum,@Param("numPerPage")int numPerPage);
	
	/**
	 * 分页查询返回总数量
	 * <pre>
	 * 	例如：
	 *  Map<String,Object> where = new HashMap<String,Object>();
	 *  param.put("orderby", "param.name,param age desc");
	 * </pre>
	 * @param where 参数为where约束参数
	 * @return
	 */
	@SelectProvider(type=SysUserSqlProvider.class,method="queryPagerCountSql")
	public Long queryPagerCount(@Param("_where")Map<String,Object> where);
	//-------------------------//end--------------------------------------------
	/**
	* @Title: existUnionId
	* @Description: 判断用户是否已经存在
	* @return boolean    返回类型
	 */
	@Select("SELECT COUNT(*) FROM xbdun_sys_user WHERE unionid=#{unionId}")
	public int existUnionId(@Param("unionId")String unionId);

	/**
	* @Title: existIdCard
	* @Description: 判断身份证号是否已经存在
	* @param idCard
	* @return    设定文件
	* @return boolean    返回类型
	*/
	@Select("SELECT COUNT(*) FROM xbdun_sys_user WHERE idCard=#{idCard}")
	public int existIdCard(@Param("idCard")String idCard);

	/**
	* @Title: getToOrgCnt
	* @Description 查询用户归属机构
	* @param userCode
	* @return    设定文件
	* @return int    返回类型
	 */
	@Select("SELECT COUNT(*) FROM xbdun_org_user WHERE userCode = #{userCode}")
	public int getToOrgCnt(@Param("userCode")long userCode);

	/**********************************查询 *******************************************/
	/**
	 * 分页查询 
	 * <pre>
	 * 	例如：
	 *  Map<String,Object> where = new HashMap<String,Object>();
	 *  param.put("orderby", "param.name,param age desc");
	 * </pre>
	 * @param _where where条件，现在支持And条件类型
	 * @param orderBy 排序条件
	 * @param pageNum 当前索引页
	 * @param numPerPage 每页显示条数
	 * @return
	 */
	@SelectProvider(type=SysUserSqlProvider.class,method="querySysUserPagerSql")
	public List<SysUserDto> querySysUserPager(@Param("_where")Map<String,Object> where,@Param("_orderby")String orderBy,@Param("pageIndex")int pageIndex,@Param("pageSize")int pageSize);
	
	/**
	 * 分页查询 
	 * <pre>
	 * 	例如：
	 *  Map<String,Object> where = new HashMap<String,Object>();
	 *  param.put("orderby", "param.name,param age desc");
	 * </pre>
	 * @param _where where条件，现在支持And条件类型
	 * @param orderBy 排序条件
	 * @param pageNum 当前索引页
	 * @param numPerPage 每页显示条数
	 * @return
	 */
	@SelectProvider(type=SysUserSqlProvider.class,method="queryOrgUserPagerSql")
	public List<SysUserDto> queryOrgUserPager(@Param("_where")Map<String, Object> where, @Param("_orderby")String orderBy, @Param("pageIndex")int pageIndex, @Param("pageSize")int pageSize);
	/**
	 * 分页查询返回总数量
	 * <pre>
	 * 	例如：
	 *  Map<String,Object> where = new HashMap<String,Object>();
	 *  param.put("orderby", "param.name,param age desc");
	 * </pre>
	 * @param where 参数为where约束参数
	 * @return
	 */
	@SelectProvider(type=SysUserSqlProvider.class,method="queryOrgUserPagerCountSql")
	public Long queryOrgUserPagerCount(@Param("_where")Map<String, Object> where);

	/**用户被邀请后修改用户信息**/
	@Update("UPDATE xbdun_sys_user SET unionid=#{unionId},xcxOpenId=#{openId},longitude=${longitude},latitude=${latitude} WHERE userCode=${userCode} AND (unionid is null OR unionid = '')")
	public int updateUserByInvite(@Param("userCode")long userCode,@Param("unionId")String unionId,@Param("openId")String openId,@Param("latitude")BigDecimal latitude,@Param("longitude")BigDecimal longitude);

	//更新平台用户
	public void updateUserInfo(@Param("p")Map<String, Object> param, @Param("w")Map<String, Object> where);

}